package com.gaohe.scp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gaohe.common.utils.DateUtils;
import com.gaohe.scp.domain.CsNoticeContent;
import com.gaohe.scp.domain.vo.CsNoticeVo;
import com.gaohe.scp.mapper.CsNoticeContentMapper;
import org.springframework.beans.BeanUtils;
import com.gaohe.scp.domain.CsNoticeContent;
import com.gaohe.scp.mapper.CsNoticeContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gaohe.scp.mapper.CsNoticeMapper;
import com.gaohe.scp.domain.CsNotice;
import com.gaohe.scp.service.ICsNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统通知Service业务层处理
 *
 * @author liuxiangyu
 * @date 2025-03-25
 */
@Service
public class CsNoticeServiceImpl implements ICsNoticeService
{
    @Autowired
    private CsNoticeMapper csNoticeMapper;
    @Autowired
    private CsNoticeContentMapper csNoticeContentMapper;


    /**
     * 查询系统通知
     *
     * @param noticeId 系统通知主键
     * @return 系统通知
     */
    @Override
    public CsNotice selectCsNoticeByNoticeId(Long noticeId)
    {
        return csNoticeMapper.selectCsNoticeByNoticeId(noticeId);
    }

    /**
     * 查询系统通知
     * 张文豪
     * @param noticeId 系统通知主键
     * @return 系统通知
     */
    @Override
    public Map<String,Object> selectCsNoticeByNoticeIdMap(Long noticeId)
    {
        CsNotice csNotice = csNoticeMapper.selectCsNoticeByNoticeId(noticeId);
        CsNoticeContent csNoticeContent = csNoticeContentMapper.selectCsNoticeContent(noticeId);
        String content = csNoticeContent.getContent();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.convertValue(csNotice,Map.class);
        map.put("content",content);
        return map;
    }

    /**
     * 查询系统通知列表
     *
     * @param csNotice 系统通知
     * @return 系统通知
     */
    @Override
    public List<CsNotice> selectCsNoticeList(CsNotice csNotice)
    {
        return csNoticeMapper.selectCsNoticeList(csNotice);
    }

    /**
     * 新增系统通知
     *
     * @param csNotice 系统通知
     * @return 结果
     */
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public int insertCsNotice(CsNotice csNotice,String content)
    {
        CsNoticeContent csNoticeContent = new CsNoticeContent();
        csNotice.setCreateTime(DateUtils.getNowDate());
        csNoticeContent.setCreateTime(DateUtils.getNowDate());
        int i = csNoticeMapper.insertCsNotice(csNotice);
        Long noticeId = csNotice.getNoticeId();
        csNoticeContent.setNoticeId(noticeId);
        csNoticeContent.setContent(content);
        int j = csNoticeContentMapper.insertCsNoticeContent(csNoticeContent);
        if (i==0 || j==0){
            return 0;
        }else {
            return i+j;
        }
    }

    /**
     * 修改系统通知
     *
     * @param csNotice 系统通知
     * @return 结果
     */
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public int updateCsNotice(CsNotice csNotice,String content)
    {
        CsNoticeContent csNoticeContent = new CsNoticeContent();
        csNotice.setUpdateTime(DateUtils.getNowDate());
        csNoticeContent.setUpdateTime(DateUtils.getNowDate());
        int i = csNoticeMapper.updateCsNotice(csNotice);
        Long noticeId = csNotice.getNoticeId();
        csNoticeContent.setNoticeId(noticeId);
        csNoticeContent.setContent(content);
        int j = csNoticeContentMapper.updateCsNoticeContent(csNoticeContent);
        if (i==0 || j==0){
            return 0;
        }else {
            return i+j;
        }
    }

    /**
     * 批量删除系统通知
     *
     * @param noticeIds 需要删除的系统通知主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCsNoticeByNoticeIds(Long[] noticeIds)
    {
        int i = csNoticeMapper.deleteCsNoticeByNoticeIds(noticeIds);
        int j = csNoticeContentMapper.deleteCsNoticeContentByContentIds(noticeIds);
        if (i==0 || j==0){
            return 0;
        }else {
            return i+j;
        }
    }

    /**
     * 删除系统通知信息
     *
     * @param noticeId 系统通知主键
     * @return 结果
     */
    @Override
    public int deleteCsNoticeByNoticeId(Long noticeId)
    {
        return csNoticeMapper.deleteCsNoticeByNoticeId(noticeId);
    }

    @Override
    public List<CsNotice> getNoticeByUserId(Long noticeId) {
        return csNoticeMapper.getNoticeByUserId(noticeId);
    }
//轮播图
    @Override
    public List<CsNoticeVo> selectAllNoticeImage(){
    //整体思路：已知是查询公司的轮播图，公司字段在notice表里面，轮播图在notice_content表里面，所以先创建出notice,
        //设置查询的属性

        //查询条件,获取type为公司的信息
        CsNotice csNotice = new CsNotice();
        csNotice.setType("公司");
        List<CsNotice> csNotices = csNoticeMapper.selectCsNoticeList(csNotice);
        //创建一个空集合noticeVo，准备把找到的信息放到noticeVo里面
        List<CsNoticeVo> csNoticeVos = new ArrayList<>();
        for (CsNotice notice : csNotices) {
            //获取notice的信息
            Long noticeId = notice.getNoticeId();
            //创建一个空集合CsNoticeContent，准备把找到的信息放到CsNoticeContent里面
            CsNoticeContent csNoticeContent = new CsNoticeContent();
            //content表与notice表都有noticeId字段，contnet可根据noticeId获得自己的noticeId
            csNoticeContent.setNoticeId(noticeId);

            //根据noticeId查询content表里面的信息
            CsNoticeContent noticeContent = csNoticeContentMapper.selectCsNoticeContentList(csNoticeContent).get(0);
            //创建csNoticeVo，把信息从noticeContent获取过来
            CsNoticeVo csNoticeVo = new CsNoticeVo();
            BeanUtils.copyProperties(notice,csNoticeVo);
            csNoticeVo.setContent(noticeContent.getContent());
            //添加数据
            csNoticeVos.add(csNoticeVo);

        }

        return csNoticeVos;
    }
    //通过日期获取新闻（滚动条）
    @Override
    public List<String> selectNoticeNews() {
        Date nowDate = DateUtils.getNowDate();
        System.err.println(nowDate);
        List<String> strings = csNoticeMapper.selectNoticeNews();
        System.err.println(strings);
        return strings;
    }
//消息
    @Override
    public List<CsNoticeVo> selectCsNotice(Long userId) {
        return csNoticeMapper.selectCsNotice(userId);
    }


}
